<?php
/*
评论的管理模块
*/
define('SCR','comment');
require_once 'global.php';
require_once(R_P . 'require/init.php');
require_once 'require/chinese.php';

$com = new Comment();

class Comment{
	var $mid;
	var $tid;
	var $cid;
	/**
	 * 一次显示的评论条目
	 *
	 * @var integer
	 */
	var $numSize=10;
	/**
	 * 当前页次
	 *
	 * @var integer
	 */
	var $pageNo;
	var $facedb;

	function __construct()
	{
		global $job,$pageno;
		!$job && $job='main';
		$this->pageNo = intval($pageno);
		$this->pageNo<1 && $this->pageNo=1;
		$this->$job();
	}

	function Comment()
	{
		$this->__construct();
	}

	function main()
	{
		$shownum	= intval($_GET['shownum']);
		$this->tid	= intval($_GET['tid']);
		$this->mid	= intval($_GET['mid']);
		$page<1 && $page = 1;
		$this->showMessage();
	}

	/**
	 * 显示评论信息内容
	 * @param integer page 当前显示的页数
	 * @return string 经过编码处理的的评论部分信息
	 */
	function showMessage()
	{
		global $db,$very, $login_name;
		//exit($login_name);
		$rt = $db->get_one("SELECT comnum FROM cms_contentindex WHERE tid='$this->tid'");
		$total = $rt['comnum'];
		require_once(R_P.'require/ajax_page.php');
		require_once(D_P.'data/cache/face.php');
		$this->facedb = $facedb;
		$query = "comment.php?job=main&tid=$this->tid&mid=$this->mid&pageno";
		$pager = new pager($total,$this->numSize,$query);
		$pages = $pager->getPageSet();
		$start = ($this->pageNo-1)*$this->numSize;
		$rs = $db->query("SELECT * FROM cms_comment WHERE tid='$this->tid' AND mid='$this->mid' ORDER BY postdate DESC LIMIT $start,$this->numSize");
		$result = array();
		while ($com = $db->fetch_array($rs))
		{
			//$com['postdate'] = get_date($com['postdate']);
			if(!$com['author']){
				$authorip = explode('.',$com['fromip']);
				array_pop($authorip);
				array_pop($authorip);
				$authorip[]='*';
				$authorip[]='*';
				$com['author'] = implode('.',$authorip);
			}
			$com['message'] = preg_replace("/\[:([0-9]+):\]/e","Comment::commentFace('\\1')",$com['message']);
			$result[] = $com;
		}
		start('utf-8');
		require Template();
		$content = ob_get_contents();
		ob_end_clean();
		$content = $this->toutf8($content);
		$content = str_replace(array("::wind::"),array("<br />"),$content);
		exit($content);
	}

	function pub(){
		echo "400"; //为将来登录做准备来返回一个登录状态
		exit();
	}

	function addMsg(){
		global $db,$timestamp,$onlineip,$very;
		if($very['ckcomment']){
			$ck = strtolower($_POST['ck']);
			GdConfirm($ck);
		}
		$author = $this->toutf8(Char_cv($_POST['author']));
		$message = $this->toutf8(Char_cv($_POST['message']));
		empty($message) && die('0');
		if(empty($_COOKIE) || $timestamp-GetCookie('comment')<10) die('wait'); //10秒内只允许评论一次
		Cookie('comment',$timestamp);
		$this->tid = (int)$_POST['tid'];
		$this->cid = (int)$_POST['cid'];
		$this->mid = (int)$_POST['mid'];
		require_once(D_P.'data/cache/cate_'.$this->cid.'.php');
		if(!$cateinfo['comment']){
			throwError('commentnotallow');
		}
		$db->update("INSERT INTO cms_comment SET
			tid='$this->tid',
			mid='$this->mid',
			author='$author',
			message='$message',
			postdate='$timestamp',
			fromip='$onlineip'
		");
		if($this->mid >0){
			$db->update("UPDATE cms_contentindex SET comnum=comnum+1 WHERE tid='$this->tid' AND cid='$this->cid'");
		}
		$this->showMessage();
	}

	function delMsg(){
		global $db,$id;
		if(!is_array($id)){
			$ids = (int)$id;
			$num = 1;
		}else{
			$ids = array();
			foreach ($id as $i){
				$ids[] = intval($i);
			}
			unset($id);
			$num = count($ids);
			$ids = implode(',',$ids);
		}
		$db->update("DELETE FROM cms_comment WHERE id IN($ids)");
		$db->update("UPDATE cms_contentindex SET comnum=comnum-$num WHERE tid='$this->tid'");

	}

	/**
	 * 将输出的内容转换为utf-8格式
	 *
	 * @param string $str
	 * @return string
	 */
	function toutf8($str){
		global $charset;
		if($charset != 'utf8'){
			$chs = new Chinese($charset,'UTF8');
			$str=$chs->Convert($str);
		}
		return $str;
	}

	function toGbk($str){
		global $charset;
		if($charset != 'utf8'){
			$chs = new Chinese('UTF8',$charset);
			$str=$chs->Convert($str);
		}
		return $str;
	}

	function commentFace($num){
		return "<img src='images/comment/".$this->facedb[$num]['facepath']."'>";
	}
}
?>